import student
from student import *
# import os
class StudentManage(object):
    def __init__(self):
        self.student_list = []

    # 程序入口
    def run(self):
        self.load_student()
        while True:
            self.show_menu()
            num = int(input("请输入您需要的功能序号:"))
            if num == 1:
                self.add_student()
            elif num ==2:
                self.del_student()
            elif num == 3:
                self.modify_student()
            elif num == 4:
                self.search_student()
            elif num == 5:
                self.show_student()
            elif num == 6:
                self.save_students()
            elif num == 7:
                break
    # 显示功能菜单
    @staticmethod
    def show_menu():
        print('请选择如下功能:')
        print('1:添加学员')
        print('2:删除学员')
        print('3:修改学员信息')
        print('4:查询学员信息')
        print('5:显示所有学员信息')
        print('6:保存学员信息')
        print('7:退出系统')
    # 添加学员
    def add_student(self):
        name = input('请输入学员姓名')
        gender = input('请输入学员性别')
        tel = input('请输入学员手机号')
        # 查找是否存在
        for s in self.student_list:
            if s.name == name:
                print('学员已经存在')
                break;
        else:
            st = Student(name,gender,tel)
            self.student_list.append(st)
    # 删除学员
    def del_student(self):
        name = input('请输入学员姓名')
        for s in self.student_list:
            if s.name == name:
                self.student_list.remove(s)
                print('学员已经删除')
                break
        else:
            print('学员不存在')
    # 修改学员
    def modify_student(self):
        modify_name = input('请输入要修改学员姓名:')
        for s in self.student_list:
            if s.name == modify_name:
                s.name = input('请输入新的姓名:')
                s.gender = input('请输入新的性别:')
                s.tel = input('请输入新的年龄')
                print('学员已经修改')
                break
        else:
            print('学员不存在')
    # 查询学员信息
    def search_student(self):
        name = input('请输入需要查询的学员姓名')
        for s in self.student_list:
            if s.name == name:
                print(f'该学员信息为{s}')
                break
        else:
            print('学员不存在')
    # 查询所有学员信息
    def show_student(self):
        # for s in self.student_list:
        # print(self.student_list)
        for s in self.student_list:
            print(f'姓名{s.name},性别{s.gender},手机号{s.tel}')
    # 保存学员信息
    def save_students(self):
        if len(self.student_list)==0:
            print('保存成功')
        else:
            f = open('student.data','w')
            new_list = [i.__dict__ for i in self.student_list]
            f.write(str(new_list))
            f.close()
    def load_student(self):
        try:
            f = open('student.data','r')
        except:
            f = open('student.data','w')
        else:
            data = f.read()
            new_list = eval(data)
            self.student_list = [Student(i['name'],i['gender'],i['tel']) for i in new_list]
        finally:
            f.close()

if __name__ == '__main__':
    st = Student('ba', 'nan', '123456789')
    print(st)